﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using System.BitConverter;

namespace WebService_Newstore.DAO
{
    public class EmplacementDAO : BaseDAO
    {
        public int id;
        public string lib;
        public bool isPicking;
        public float stockMax;
        public float stockAlerte;
        public float stockQte;
        public int idUnite;
        public int idArticle;

        public EmplacementDAO() { }

        public EmplacementDAO(int id, string lib, bool ispick, float stMax, float stAlerte, float StQte, int idUnit, int idArt)
        {
            this.id = id;
            this.lib = lib;
            this.isPicking = ispick;
            this.stockMax = stMax;
            this.stockAlerte = stAlerte;
            this.stockQte = StQte;
            this.idUnite = idUnit;
            this.idArticle = idArt;
        }

        public static List<EmplacementDAO> getEmplacementByArticleID(int articleID,string codeUnite)
        {
            List<EmplacementDAO> listToSend = new List<EmplacementDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                int idUnite = 0;
                switch (codeUnite)
                {
                    case "P": idUnite = 1; break;
                    case "PAL": idUnite = 2; break;
                }

                try
                {
                    var query = from emplacement in newStoreContext.emplacements
                                where emplacement.id_article == articleID && emplacement.id_unite == idUnite
                                select emplacement;

                    if (query.LongCount() == 0)
                        throw new Exception("Erreur, aucun emplacement trouvé !!");

                    //N'en retourne qu'un si il n'y a pas d'erreur de saisie dans la base
                    foreach (var emp in query)
                    {
                        EmplacementDAO emplacementDAO = new EmplacementDAO();
                        emplacementDAO.id = emp.id_emp;
                        emplacementDAO.lib = emp.lib_emp;
                        foreach (byte byteValue in emp.is_picking)
                        {
                            emplacementDAO.isPicking = Convert.ToBoolean(byteValue);
                        }
                        emplacementDAO.stockMax = (float)emp.stMax_emp;
                        emplacementDAO.stockAlerte = (float)emp.stAlerte_emp;
                        emplacementDAO.stockQte = (float)emp.stQte_emp;
                        emplacementDAO.idUnite = (int)emp.id_unite;
                        emplacementDAO.idArticle = (int)emp.id_article;
                        listToSend.Add(emplacementDAO);

                    }
                    return listToSend;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    return listToSend;
                }
            }
        }

        public static EmplacementDAO getEmplacementByID(int empID)
        {
            EmplacementDAO emplacementDAO = new EmplacementDAO();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from emplacement in newStoreContext.emplacements
                                where emplacement.id_emp == empID
                                select emplacement;

                    if (query.LongCount() != 1)
                        throw new Exception("Emplacement inexistant");

                    //N'en retourne qu'un si il n'y a pas d'erreur de saisie dans la base
                    foreach (var emp in query)
                    {
                        emplacementDAO.id = emp.id_emp;
                        emplacementDAO.lib = emp.lib_emp;
                        //emplacementDAO.isPicking = (bool)emp.is_picking;
                        foreach (byte byteValue in emp.is_picking)
                        {
                            emplacementDAO.isPicking = Convert.ToBoolean(byteValue);
                        }
                        emplacementDAO.stockMax = (float)emp.stMax_emp;
                        emplacementDAO.stockAlerte = (float)emp.stAlerte_emp;
                        emplacementDAO.stockQte = (float)emp.stQte_emp;
                        emplacementDAO.idUnite = (int)emp.id_unite;
                        emplacementDAO.idArticle = (int)emp.id_article;

                    }
                    return emplacementDAO;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    return emplacementDAO;
                }
            }
        }

        public static EmplacementDAO SaveEmplacementDAO(EmplacementDAO empDAO)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    emplacement em = newStoreContext.emplacements.FirstOrDefault(i => i.id_emp == empDAO.id);
                    if (em != null)
                    {
                        //Modification
                        em.id_emp = empDAO.id;
                        em.lib_emp = empDAO.lib;
                        em.is_picking = BitConverter.GetBytes(empDAO.isPicking);
                        em.stAlerte_emp = empDAO.stockAlerte;
                        em.stMax_emp = empDAO.stockMax;
                        em.stQte_emp = empDAO.stockQte;
                        em.id_unite = empDAO.idUnite;
                        em.id_article = empDAO.idArticle;
                        newStoreContext.SaveChanges();
                        return empDAO;
                    }
                    else
                    {
                        //Insertion
                        emplacement emp = new emplacement();
                        emp.lib_emp = empDAO.lib;
                        emp.is_picking = BitConverter.GetBytes(empDAO.isPicking);
                        emp.stAlerte_emp = empDAO.stockAlerte;
                        emp.stMax_emp = empDAO.stockMax;
                        emp.stQte_emp = empDAO.stockQte;
                        emp.id_unite = empDAO.idUnite;
                        emp.id_article = empDAO.idArticle;
                        newStoreContext.emplacements.AddObject(emp);
                        newStoreContext.SaveChanges();
                        empDAO.id = emp.id_emp;
                        return empDAO;
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException);
                    return empDAO;
                }
            }
        }

    }
}
